using System.IO;
using LogSystem;
using UnityEditor;
using UnityEngine;

namespace Yoozoo.Arts.Editor
{
    public static class UITool
    {
        [MenuItem("GTATools/UI/精灵移动到Arts目录下")]
        public static void MoveToArts()
        {
            var objects = Selection.objects;
            if (objects == null)
                return;

            foreach (var o in objects)
            {
                var assetPath = AssetDatabase.GetAssetPath(o);
                if (assetPath.Contains("UI/GUI"))
                    MoveTo(assetPath, "Assets/Arts/Misc/UI_Textures");
            }
        }

        [MenuItem("GTATools/UI/精灵移动到GUIBig目录下")]
        public static void MoveToBig()
        {
            var objects = Selection.objects;
            if (objects == null)
                return;

            foreach (var o in objects)
            {
                var assetPath = AssetDatabase.GetAssetPath(o);
                if (assetPath.Contains("UI/GUI"))
                    MoveTo(assetPath, "Assets/ResourcesAssets/UI/GUIBig");
            }
        }

        [MenuItem("GTATools/UI/预制体移动到Arts目录下")]
        public static void MovePrefabToArts()
        {
            var objects = Selection.objects;
            if (objects == null)
                return;

            foreach (var o in objects)
            {
                var assetPath = AssetDatabase.GetAssetPath(o);
                if (assetPath.Contains("Prefabs/UI"))
                    MoveTo(assetPath, "Assets/Arts/Misc/UI_Prefabs");
            }
        }

        private static void MoveTo(string assetPath, string targetDirectory)
        {
            var directoryName = Path.GetDirectoryName(assetPath);
            if (string.IsNullOrEmpty(directoryName))
                return;

            var directoryInfo = new DirectoryInfo(directoryName);
            string folderName = directoryInfo.Name;
            string newDirectory = $"{targetDirectory}/{folderName}";
            if (!AssetDatabase.IsValidFolder(newDirectory))
                AssetDatabase.CreateFolder(targetDirectory, folderName);

            string spriteFileName = Path.GetFileName(assetPath);
            string newPath = $"{newDirectory}/{spriteFileName}";
            string error = AssetDatabase.MoveAsset(assetPath, newPath);
            if (string.IsNullOrEmpty(error))
                Debug.Log(LogModule._Default_, "Done");
            else
                Debug.LogError(LogModule._Default_, error);
        }
    }
}